package com.bub.pay.api.filter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.bub.pay.framework.common.base.Cause;
import com.bub.pay.framework.common.exception.GlobalException;
import com.bub.pay.core.manager.user.UserCommonManager;
import com.bub.pay.framework.common.entity.model.SessionModel;

public class BindInterceptor extends HandlerInterceptorAdapter {

	@Resource
	private UserCommonManager userManager;

	@Override
	public boolean preHandle(HttpServletRequest httpRequest, HttpServletResponse httpResponse, Object handler)
			throws Exception {
		final String userId = httpRequest.getHeader("UserId");
		SessionModel session = userManager.getSession(userId);
		if (session.getResetFlag() == 1)
			throw new GlobalException(Cause.error_auth_reset);
		if (session.getBindGoogle() == 0)
			throw new GlobalException(Cause.error_auth_binGoogle);
		if (session.getBindPayPass() == 0 && session.getUserType() != 2)
			throw new GlobalException(Cause.error_auth_binPayPass);
		return super.preHandle(httpRequest, httpResponse, handler);
	}

}
